﻿using System;
using System.Globalization;
using AnjLab.FX.IO;
using AnjLab.FX.Log4Net;
using AnjLab.FX.Sys;

namespace AnjLab.SyncIT.Core
{
    public class Library
    {
        public static readonly ILog Log = NewNamespaceLog(typeof (Library));
        public static readonly CultureInfo Culture = CultureInfo.CurrentCulture;
        public static readonly CultureInfo InvariantCulture = CultureInfo.InvariantCulture;

        static Library()
        {
            Log4NetLog.LoadLog4NetConfig();
        }

        public static ILog NewNamespaceLog(Type anyTypeInNamespace)
        {
            Guard.ArgumentNotNull("anyTypeInNamespace", anyTypeInNamespace);
            return new Log4NetLog(anyTypeInNamespace.Namespace);
        }

        public static string HumanizeSyncInterval(TimeSpan duration, CultureInfo culture)
        {
            if (duration.TotalMinutes == 0)
                return "Do not autosync";

            if (duration.TotalMinutes == 30)
                return "Sync every half hour";

            if (duration.TotalMinutes < 60)
                return string.Format(culture, "Sync every {0} minute(s)", duration.TotalMinutes);

            if (duration.TotalMinutes == 60)
                return string.Format("Sync every hour");

            if (duration.Minutes == 0 || duration.Minutes == 30)
                return string.Format(culture, "Sync every {0} hours", duration.TotalHours);

            return string.Format(culture, "Sync every {0}h {1}m", (int)duration.TotalHours, duration.Minutes);
        }

        public static string HumanizeSyncInterval(TimeSpan duration) {
            return HumanizeSyncInterval(duration, Culture);
        }
    }
}
